package pkg16b_eca.data.validate;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTextField;

public class Text {
    /**When you want to parse a number, either it's a number or it's not and if it isn't a number it'll return -1. */
    public static int parseNumber(String string) {
        //Check for validation errors.
        if (string == null || string.isEmpty()) return -1;
        try {
                //If it's a number, then return the number.
                return Integer.parseInt(string);
            }
        //If it isn't a number, the exception will be thrown and the function will return -1.
        catch (NumberFormatException exception) {
            //System.out.println(exception.getMessage());
            return -1;
        }
    }
    
    /**This function return either true or false, whether the string is a number or not. */
    public static boolean isNumber(String string) {
        //Check for validation errors.
        if (string == null || string.isEmpty()) return false;
        try {
            //Checking if it's a number
            Integer.parseInt(string);
            //Return true if it is a number.
            return true;
        }
        //If the exception were thrown it isn't a number.
        catch (NumberFormatException exception) {
            //System.out.println(exception.getMessage());
        }
        return false; //Return false if it isn't a number.
    }
    
    /**Used for controls using JTextField, making the text red if the input is incorrect. */
    public static void isNumberColoredError(JTextField textbox) {
        if (textbox == null) return; //Checking if it's null.
        //If the textbox's text is empty or a number, the text will be black.
        if (textbox.getText().isEmpty() || Text.isNumber(textbox.getText())) {
            textbox.setForeground(Color.black);
        }
        else {
            //If it isn't only numbers, for example if you type a word or something, the text will become red and the user will be warned.
            textbox.setForeground(Color.red);
        }
    }
    
    /**Replacing the characters to get å, ä, ö, Å, Ä and Ö from ASCII characters. So the database values that are being saved need to go through the method getSafeSpecialCharacters([String]). */
    public static String getSpecialCharacters(String string) {
        //Checking for validation errors.
        if (string == null || string.isEmpty()) return "";
        
        /*string = string.replace('å', (char) 229);
        string = string.replace('Å', (char) 197);
        string = string.replace('ä', (char) 228);
        string = string.replace('Ä', (char) 196);
        string = string.replace('ö', (char) 246);
        string = string.replace('Ö', (char) 214);*/
        
        //Replacing characters...
        string = string.replace("&#229", "å");
        string = string.replace("&#197", "Å");
        string = string.replace("&#228", "ä");
        string = string.replace("&#196", "Ä");
        string = string.replace("&#246", "ö");
        string = string.replace("&#214", "Ö");
        
        //Returns the actual characters that we understand.
        return string;
    }
    
    /**Replacing characters so they can be saved in the database in a safe way. Even if you can save these characters in the database not converted. */
    public static String getSafeSpecialCharacters(String string) {
        //Checking for validation errors.
        if (string == null || string.isEmpty()) return "";
        
        //Replacing the characters...
        string = string.replace("å", "&#229");
        string = string.replace("Å", "&#197");
        string = string.replace("ä", "&#228");
        string = string.replace("Ä", "&#196");
        string = string.replace("ö", "&#246");
        string = string.replace("Ö", "&#214");
        //string = string.replace("'", "");
        
        //Returns a safe string
        return string.trim();
    }
    
    public static Date parseDate(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
            java.util.Date format = sdf.parse(date);
            return new java.sql.Date(format.getTime());
        }
        catch(Exception e) {
            return new Date();
        }
    }
}
